package servlet;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import model.Administrator;
import model.User;
import service.AdminstratorService;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "AdminstratorServlet", value = "/AdminstratorServlet")
public class AdminstratorServlet extends HttpServlet {
    private AdminstratorService adminstratorService=new AdminstratorService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        //告诉浏览器，服务器发送的消息体的数据的编码
        response.setHeader("content-type", "text/html;charset=utf-8");

        PrintWriter pw = response.getWriter();
        String account = request.getParameter("account");
        String password = request.getParameter("password");
//        System.out.println(account+password+"测试测试");
        Administrator administrator = adminstratorService.Login(account, password);
//        System.out.println(administrator+"测试测试测试测试测试");//测试
        if (administrator == null) {
            pw.print("<script>alert('账号或者/密码错误~~~~'); window.location='Administrator.jsp' </script>");
        } else {
//            System.out.println(user);//测试
            request.getSession().setAttribute("administrator", administrator);
            pw.print("<script>alert('管理员登录成功,欢迎您~!" + administrator.getAccount() + "'); window.location='userlistAdmin.jsp' </script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
